package kyle.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class HttpRequest {

	private static final String APPLICATION_JSON = "application/json";
	private static final String CONTENT_TYPE_TEXT_JSON = "text/json";

	public static String doGet(String urlAdress) {
		HttpGet httpGet = new HttpGet(urlAdress);

		// HttpParams hp = httpGet.getParams();
		// hp.getParameter("true");
		// hp.
		// httpGet.setp
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 50000);
		HttpClient hc = new DefaultHttpClient(params);

		try {
			HttpResponse ht = hc.execute(httpGet);
			if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity he = ht.getEntity();
				InputStream is = he.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String response = "";
				String readLine = null;
				while ((readLine = br.readLine()) != null) {
					// response = br.readLine();
					response = response + readLine;
				}
				is.close();
				br.close();

				// String str = EntityUtils.toString(he);
				System.out.println("=========" + response);
				return response;
			} else {
				return "error";
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}

	public static String doPostWithMultiData(String url, String json, File file) {
		HttpPost httpPost = new HttpPost(url);
		// MultipartRequestEntity entity = new MultipartRequestEntity();
		MultipartEntity entity = new MultipartEntity();
		if (file != null) {
			ContentBody content = new FileBody(file);
			entity.addPart("file", content);
		}
		try {

			entity.addPart("data", new StringBody(json));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		httpPost.setEntity(entity);
		HttpParams httpparams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpparams, 50000);
		HttpClient hc = new DefaultHttpClient(httpparams);
		try {
			HttpResponse ht = hc.execute(httpPost);
			// 连接成功
			if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity het = ht.getEntity();
				InputStream is = het.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String response = "";
				String readLine = null;
				while ((readLine = br.readLine()) != null) {
					// response = br.readLine();
					response = response + readLine;
				}
				is.close();
				br.close();

				// String str = EntityUtils.toString(he);
				System.out.println("=========&&" + response);
				return response;
			} else {
				return "error";
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
	}

	public static String uploadFile(String url, File file) {
		HttpPost httpPost = new HttpPost(url);
		// MultipartRequestEntity entity = new MultipartRequestEntity();
		MultipartEntity entity = new MultipartEntity();
		ContentBody content = new FileBody(file);
		entity.addPart("file", content);
		httpPost.setEntity(entity);
		HttpParams httpparams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpparams, 50000);
		HttpClient hc = new DefaultHttpClient(httpparams);
		try {
			HttpResponse ht = hc.execute(httpPost);
			// 连接成功
			if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity het = ht.getEntity();
				InputStream is = het.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String response = "";
				String readLine = null;
				while ((readLine = br.readLine()) != null) {
					// response = br.readLine();
					response = response + readLine;
				}
				is.close();
				br.close();

				// String str = EntityUtils.toString(he);
				System.out.println("=========&&" + response);
				return response;
			} else {
				return "error";
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}

	}

	public static String doPostWithJSON(String url, String json) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		NameValuePair pair1 = new BasicNameValuePair("data", json);
		params.add(pair1);
		return doPost(url, params);
	}

	public static String doPost(String urlAddress, List params) {
		HttpPost httpPost = new HttpPost(urlAddress);
		// List params = new ArrayList();
		// NameValuePair pair1 = new BasicNameValuePair("username", username);
		// NameValuePair pair2 = new BasicNameValuePair("password", password);
		// params.add(pair1);
		// params.add(pair2);

		HttpEntity he;
		try {
			he = new UrlEncodedFormEntity(params, "utf-8");
			httpPost.setEntity(he);

		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		HttpParams httpparams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpparams, 50000);
		HttpClient hc = new DefaultHttpClient(httpparams);
		try {
			HttpResponse ht = hc.execute(httpPost);
			// 连接成功
			if (ht.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity het = ht.getEntity();
				InputStream is = het.getContent();
				BufferedReader br = new BufferedReader(
						new InputStreamReader(is));
				String response = "";
				String readLine = null;
				while ((readLine = br.readLine()) != null) {
					// response = br.readLine();
					response = response + readLine;
				}
				is.close();
				br.close();

				// String str = EntityUtils.toString(he);
				System.out.println("=========&&" + response);
				return response;
			} else {
				return "error";
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "exception";
		}
	}

	public static String httpPostWithJSON(String url, String json) {
		// 将JSON进行UTF-8编码,以便传输中文
		try {
			String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost httpPost = new HttpPost(url);
			httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);

			StringEntity se;
			se = new StringEntity(json);
			se.setContentType(CONTENT_TYPE_TEXT_JSON);
			se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					APPLICATION_JSON));
			httpPost.setEntity(se);
			HttpResponse response;
			response = httpClient.execute(httpPost);
			HttpEntity entity = response.getEntity();
			BufferedReader bufferedReader;
			bufferedReader = new BufferedReader(new InputStreamReader(
					entity.getContent(), "UTF-8"), 8 * 1024);
			StringBuilder entityStringBuilder = new StringBuilder();
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				entityStringBuilder.append(line + "/n");
			}
			return entityStringBuilder.toString();

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
